package com.ruoyi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.common.EsIndex;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.enums.SensitiveTypeEnum;
import com.ruoyi.system.service.IPoPublicOpinionService;
import com.ruoyi.system.service.IPublicOpinionEventsService;
import com.ruoyi.system.service.IPublicOpinionTopicService;
import com.ruoyi.system.service.impl.EsUtil;
import com.ruoyi.system.task.EventTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 舆情Controller
 *
 * @author ruoyi
 * @date 2021-06-07
 */
@RestController
@RequestMapping("/system/opinion")
@Api(tags = "舆情：舆情管理")
public class PoPublicOpinionController extends BaseController {
    @Autowired
    private IPoPublicOpinionService poPublicOpinionService;
    @Autowired
    private IPublicOpinionEventsService publicOpinionEventsService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private EsUtil esUtil;
    @Autowired
    private IPublicOpinionTopicService publicOpinionTopicService;
    @Autowired
    EventTask eventTask;








    /**
     * 查询事件和主题列表
     */
    @ApiOperation("查询事件和主题列表")
    @GetMapping("/eventAndTopic")
    public TableDataInfo queryEventAndTopic() {
        List<EventAndTopic> list = poPublicOpinionService.queryEventAndTopic(SecurityUtils.getLoginUser().getUser().getTenantCode());
        return getDataTable(list);
    }



    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<PoPublicOpinion> util = new ExcelUtil<>(PoPublicOpinion.class);
        return util.importTemplateExcel("舆情数据");
    }


    /**
     * 查询舆情列表（查数据库）
     */
    @ApiOperation("查询舆情列表")
    @GetMapping("/list")
    public TableDataInfo list(PoPublicOpinion poPublicOpinion) {
        startPage();
        List<PoPublicOpinion> list = poPublicOpinionService.selectPoPublicOpinionList(poPublicOpinion);
        return getDataTable(list);
    }


    /**
     * 查询舆情清单列表
     */

    @PostMapping("/queryListOpinion")
    public TableDataInfo queryListOpinion(PoPublicOpinion opinion, @RequestBody EventParameterVo parameter) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        HashMap esSearchResult = esUtil.getOpinionsByParameter(parameter, opinion, pageNum, pageSize);
        List<PoPublicOpinion> list = (List<PoPublicOpinion>) esSearchResult.get("list");

        TableDataInfo tableDataInfo = getDataTable(list);
        tableDataInfo.setTotal((Long) esSearchResult.get("total"));
        return tableDataInfo;
    }



    /**
     * 查询预警舆情列表
     */
    @ApiOperation("查询预警舆情列表")
    @PostMapping("/queryWarningList")
    public TableDataInfo queryWarningList(@RequestBody OpinionParameterVo opinionParameterVo) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        opinionParameterVo.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        opinionParameterVo.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        opinionParameterVo.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        PageInfo<PoPublicOpinion> pageInfo = poPublicOpinionService.queryWarningList(opinionParameterVo, pageNum, pageSize);
        TableDataInfo tableDataInfo = getDataTable(pageInfo.getList());
        tableDataInfo.setTotal(pageInfo.getTotal());
        return tableDataInfo;
    }


    /**
     * 导出舆情列表
     */
    @ApiOperation("导出舆情列表")
    @PreAuthorize("@ss.hasPermi('system:opinion:export')")
    @Log(title = "舆情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PoPublicOpinion poPublicOpinion) {
        poPublicOpinion.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        poPublicOpinion.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        poPublicOpinion.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PoPublicOpinion> list = poPublicOpinionService.selectPoPublicOpinionList(poPublicOpinion);
        ExcelUtil<PoPublicOpinion> util = new ExcelUtil<PoPublicOpinion>(PoPublicOpinion.class);
        return util.exportExcel(list, "舆情数据");
    }


    /**
     * 最新舆情
     */
    @ApiOperation("最新舆情")
    @Log(title = "舆情", businessType = BusinessType.EXPORT)
    @GetMapping("/newest")
    public AjaxResult queryWarningList(@RequestParam int count) {
        List<PoPublicOpinion> list = poPublicOpinionService.selectPoPublicOpinionListNewest(count);
        return AjaxResult.success(list);
    }

    /**
     * 热点舆情
     */
    @ApiOperation("热点舆情")
    @GetMapping("/hotPublicOpinion")
    public AjaxResult hotPublicOpinion(@RequestParam(value = "beginPublishDay", required = false) String beginPublishDay,
                                       @RequestParam(value = "endPublishDay", required = false) String endPublishDay,
                                       @RequestParam int count

    ) {

        List<PoPublicOpinion> list = poPublicOpinionService.hotPublicOpinion(beginPublishDay, endPublishDay, count);
        return AjaxResult.success(list);
    }


    /**
     * 负面舆情
     */
    @ApiOperation("负面舆情")
    @GetMapping("/getNegativePublicOpinion")
    public AjaxResult getNegativePublicOpinion(@RequestParam(value = "beginPublishDay", required = false) String beginPublishDay,
                                       @RequestParam(value = "endPublishDay", required = false) String endPublishDay,
                                       @RequestParam int count

    ) {

        List<PoPublicOpinion> list = poPublicOpinionService.getNegativePublicOpinion(beginPublishDay, endPublishDay, count);
        return AjaxResult.success(list);
    }


    /**
     * 根据ID导出舆情列表
     */
    @ApiOperation("根据ID导出舆情列表")
    @PreAuthorize("@ss.hasPermi('system:opinion:export')")
    @Log(title = "舆情", businessType = BusinessType.EXPORT)
    @GetMapping("/export/{ids}")
    public AjaxResult export(@PathVariable("ids") String[] ids) {
        List<PoPublicOpinion> list = poPublicOpinionService.selectPoPublicOpinionListByIds(ids);
        ExcelUtil<PoPublicOpinion> util = new ExcelUtil<>(PoPublicOpinion.class);
        return util.exportExcel(list, "舆情数据");
    }

    /**
     * 获取舆情详细信息
     */
    @ApiOperation("获取舆情详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(poPublicOpinionService.selectPoPublicOpinionById(id));
    }


    /**
     * 获取舆情热词
     */
    @ApiOperation("获取舆情热词")
    @GetMapping(value = "/getHotWords/{id}")
    public AjaxResult getHotWords(@PathVariable("id") String id) {
        return AjaxResult.success(poPublicOpinionService.getHotWords(id));
    }

    /**
     * 获取关联人物
     */
    @ApiOperation("获取关联人物")
    @GetMapping(value = "/getRelationPersonNames/{id}")
    public AjaxResult getRelationPersonNames(@PathVariable("id") String id) {
        return AjaxResult.success(poPublicOpinionService.getRelationPersonNames(id));
    }

    /**
     * 获取关联地域
     */
    @ApiOperation("获取关联地域")
    @GetMapping(value = "/getRelationLocationNames/{id}")
    public AjaxResult getRelationLocationNames(@PathVariable("id") String id) {
        return AjaxResult.success(poPublicOpinionService.getRelationLocationNames(id));
    }


    /**
     * 获取关联机构
     */
    @ApiOperation("获取关联机构")
    @GetMapping(value = "/getRelationOrganizationNames/{id}")
    public AjaxResult getRelationOrganizationNames(@PathVariable("id") String id) {
        return AjaxResult.success(poPublicOpinionService.getRelationOrganizationNames(id));
    }


    /**
     * 根据舆情查询同作者文章
     */
    @ApiOperation("根据舆情查询同作者文章")
    @GetMapping(value = "/getOpinionsOfSameAuthorById/{id}")
    public TableDataInfo getOpinionsOfSameAuthorById(@PathVariable("id") String id) {
        //分页
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        PoPublicOpinion opinion = poPublicOpinionService.selectPoPublicOpinionById(id);
        TableDataInfo tableDataInfo;
        if (StringUtils.isNotEmpty(opinion.getAuthor())) {
            PageInfo<PoPublicOpinion> pageInfo = poPublicOpinionService.getOpinionsOfSameAuthorById(id, opinion.getAuthor(), pageNum, pageSize);
            tableDataInfo = getDataTable(pageInfo.getList());
            tableDataInfo.setTotal(pageInfo.getTotal());
        } else {
            tableDataInfo = getDataTable(new ArrayList<>());
            tableDataInfo.setTotal(0);
        }
        return tableDataInfo;
    }

    /**
     * 查询相似文章
     */
    @ApiOperation("查询相似文章")
    @GetMapping(value = "/getOpinionsOfSimilarArticlesById/{id}")
    public TableDataInfo getOpinionsOfSimilarArticlesById(@PathVariable("id") String id) {
        //分页
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        PoPublicOpinion opinion = poPublicOpinionService.selectPoPublicOpinionById(id);
        PageInfo<PoPublicOpinion> pageInfo = poPublicOpinionService.getOpinionsOfSimilarArticlesById(id, opinion.getTitle(), pageNum, pageSize);
        TableDataInfo tableDataInfo = getDataTable(pageInfo.getList());
        tableDataInfo.setTotal(pageInfo.getTotal());
        return tableDataInfo;
    }


    /**
     * 新增舆情
     */
    @ApiOperation("新增舆情到数据库")
    @PreAuthorize("@ss.hasPermi('system:opinion:add')")
    @Log(title = "舆情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PoPublicOpinion poPublicOpinion) {
        poPublicOpinion.setPublishDate(new Date());
        poPublicOpinion.setPublishDay(DateUtils.parseDateToStr("yyyy-MM-dd", new Date()));
        poPublicOpinion.setPublishHour(DateUtils.parseDateToStr("HH:mm", new Date()));
        poPublicOpinion.setInsertAt(new Date());
        poPublicOpinion.setMediaType("内部整理");
        poPublicOpinion.setMediaName("内部整理");
        poPublicOpinion.setAuthor("内部整理");
        poPublicOpinion.setInsertBy("data import");
        poPublicOpinion.setTitle(StringUtils.substring(poPublicOpinion.getTitle(), 0, 20));
        poPublicOpinion.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        poPublicOpinion.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        poPublicOpinion.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        poPublicOpinionService.insertPoPublicOpinion(poPublicOpinion);
        esUtil.indexOpinion(poPublicOpinion);
        return toAjax(true);
    }

    /**
     * 修改舆情
     */
    @ApiOperation("修改舆情")
    @PreAuthorize("@ss.hasPermi('system:opinion:edit')")
    @Log(title = "舆情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PoPublicOpinion poPublicOpinion) {
        esUtil.indexOpinion(poPublicOpinion);
        return toAjax(poPublicOpinionService.updatePoPublicOpinion(poPublicOpinion));
    }

    /**
     * 舆情研判
     */
    @ApiOperation("舆情研判")
    @PreAuthorize("@ss.hasPermi('system:opinion:edit')")
    @Log(title = "舆情", businessType = BusinessType.UPDATE)
    @GetMapping("/update/sensitiveInformation/{ids}")
    public AjaxResult edit(@PathVariable String[] ids,
                           @RequestParam String sensitive) {
        return toAjax(poPublicOpinionService.updateSensitiveInformation(ids, sensitive));
    }


    /**
     * 舆情加入预警
     */
    @ApiOperation("舆情加入预警")
    @PreAuthorize("@ss.hasPermi('system:opinion:edit')")
    @Log(title = "舆情", businessType = BusinessType.INSERT)
    @GetMapping("/joinTheForecast/{ids}")
    public AjaxResult joinTheForecast(@PathVariable String[] ids,
                                      @RequestParam String level) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        String tenantCode = SecurityUtils.getLoginUser().getUser().getTenantCode();
        return toAjax(poPublicOpinionService.joinTheForecast(ids, level, userId, deptId, tenantCode));
    }

    /**
     * 删除预警舆情
     */
    @ApiOperation("删除预警舆情")
    @PreAuthorize("@ss.hasPermi('system:opinion:remove')")
    @Log(title = "舆情", businessType = BusinessType.DELETE)
    @GetMapping("/removeTheForecast/{ids}")
    public AjaxResult removeTheForecast(@PathVariable String[] ids) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        String tenantCode = SecurityUtils.getLoginUser().getUser().getTenantCode();
        return toAjax(poPublicOpinionService.removeTheForecast(ids, userId, deptId, tenantCode));
    }

    /**
     * 删除舆情
     */
    @ApiOperation("删除舆情")
    @PreAuthorize("@ss.hasPermi('system:opinion:remove')")
    @Log(title = "舆情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        esUtil.deleteDocument(EsIndex.OPINION, ids);
        return toAjax(poPublicOpinionService.deletePoPublicOpinionByIds(ids));
    }


    /**
     * 根据事件id获取舆情集合(集合分页)
     * 此方法需要实现es查询根据条件查询
     * 待优化
     */
    @ApiOperation("根据事件id获取舆情集合(集合分页)")

    @PostMapping("/queryByEventParameter/{eventId}")
    public TableDataInfoCustom queryByEventParameter(@PathVariable("eventId") Long eventId, @RequestBody EventParameterVo parameterVo) {
        //更新neo4j
//        new Thread(() -> eventTask.doNlpEvent(eventId)).start();
        //分页
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        //绑定的舆情
        List<String> bindIds = publicOpinionEventsService.getMatchOpinionByEventIdForDataBase(eventId);
        //查询事件
        PublicOpinionEvents event = publicOpinionEventsService.selectPublicOpinionEventsById(eventId);
        //es查询
        PageInfoCustom<PoPublicOpinion> opinionPageInfo = esUtil.getMatchOpinionByEventId(event, pageNum, pageSize, parameterVo, bindIds, parameterVo.getRepeatState());
        //封装返回数据
        TableDataInfo tableDataInfo = getDataTable(opinionPageInfo.getList());
        TableDataInfoCustom tableDataInfoCustom = new TableDataInfoCustom();
        tableDataInfoCustom.setCode(tableDataInfo.getCode());
        tableDataInfoCustom.setMsg(tableDataInfo.getMsg());
        tableDataInfoCustom.setTotal(opinionPageInfo.getTotal());
        tableDataInfoCustom.setRows(tableDataInfo.getRows());
        tableDataInfoCustom.setCustom(opinionPageInfo.getCustom());
        return tableDataInfoCustom;
    }


    /**
     * 根据主题id获取舆情集合(分页)
     */
    @ApiOperation("根据主题id获取舆情集合(分页)")
    @PostMapping("/queryByTopicId/{topicId}")
    public TableDataInfoCustom queryByTopicId(@PathVariable("topicId") Long topicId, @RequestBody TopicParameterVo parameterVo) {
        Long total = 0L;
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        PublicOpinionTopic topic = publicOpinionTopicService.selectPublicOpinionTopicById(topicId);
        HashMap esSearchResult = esUtil.getTopicOpinions(topic, parameterVo, pageNum, pageSize, parameterVo.getRepeatState());
        List<PoPublicOpinion> opinions = (List<PoPublicOpinion>) esSearchResult.get("opinions");
        List<NameValue> mediaTypes= (List<NameValue>) esSearchResult.get("carrie");
        total = (Long) esSearchResult.get("total");
        TableDataInfo tableDataInfo = getDataTable(opinions);
        TableDataInfoCustom tableDataInfoCustom = new TableDataInfoCustom();
        tableDataInfoCustom.setCode(tableDataInfo.getCode());
        tableDataInfoCustom.setMsg(tableDataInfo.getMsg());
        tableDataInfoCustom.setTotal(total);
        tableDataInfoCustom.setRows(tableDataInfo.getRows());
        tableDataInfoCustom.setCustom(mediaTypes);
        return tableDataInfoCustom;
    }


    /**
     * 军犬数据检索
     */
    @ApiOperation(value = "数据检索", notes = "数据检索")
    @PostMapping("/queryOpinionByDataServer")
    public AjaxResult queryOpinionByDataServer(@RequestBody SearchParameterVo parameter) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        return poPublicOpinionService.queryOpinionByDataServer(parameter, pageNum,pageSize);
    }


    /**
     * 下载线上数据到Es
     */
    @ApiOperation(value = "下载线上数据到本地", notes = "下载线上数据到Es")
    @PostMapping("/downloadDataFromServeCenter")
    public AjaxResult downloadDataFromServeCenter(@RequestBody SearchParameterVo parameter) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        return poPublicOpinionService.downloadDataFromServeCenter(parameter,pageNum,pageSize);
    }


    /**
     * 下载线上数据到本地
     */
    @ApiOperation(value = "下载线上数据到本地", notes = "下载线上数据到本地")
    @PostMapping("/downloadCsvDataFromServeCenter")
    public AjaxResult downloadCsvDataFromServeCenter(@RequestBody SearchParameterVo parameter) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        return poPublicOpinionService.downloadCsvDataFromServeCenter(parameter,pageNum, pageSize);
    }


    /**
     * 根据主题id获取舆情集合(分页)(境外)
     */
    @ApiOperation("根据主题id获取舆情集合(分页)")
    @PostMapping("/queryByTopicId/overseas/{topicId}")
    public TableDataInfoCustom queryByOverseasTopicId(@PathVariable("topicId") Long topicId, @RequestBody TopicParameterVo parameterVo) {
        Long total = 0L;
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        PublicOpinionTopic topic = publicOpinionTopicService.selectPublicOpinionTopicById(topicId);
        HashMap esSearchResult = esUtil.getTopicOpinions(topic, parameterVo, pageNum, pageSize, parameterVo.getRepeatState());
        List<PoPublicOpinion> opinions = (List<PoPublicOpinion>) esSearchResult.get("opinions");
        total = (Long) esSearchResult.get("total");
        List<OpMediaNameNumVM> mediaTypeCounts = poPublicOpinionService.getMediaOverseasNum(opinions.stream().map(PoPublicOpinion::getId).collect(Collectors.toList()));
        TableDataInfo tableDataInfo = getDataTable(opinions);
        TableDataInfoCustom tableDataInfoCustom = new TableDataInfoCustom();
        tableDataInfoCustom.setCustom(mediaTypeCounts);
        tableDataInfoCustom.setCode(tableDataInfo.getCode());
        tableDataInfoCustom.setMsg(tableDataInfo.getMsg());
        tableDataInfoCustom.setTotal(total);
        tableDataInfoCustom.setRows(tableDataInfo.getRows());
        return tableDataInfoCustom;
    }


    /**
     * 根据主题id获取舆情集合(分页)(语言)
     */
    @ApiOperation("根据主题id获取舆情集合(分页)")
    @PostMapping("/queryByTopicId/language/{topicId}")
    public TableDataInfoCustom queryByLanguageTopicId(@PathVariable("topicId") Long topicId, @RequestBody TopicParameterVo parameterVo) {
        Long total = 0L;
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        PublicOpinionTopic topic = publicOpinionTopicService.selectPublicOpinionTopicById(topicId);
        HashMap esSearchResult = esUtil.getTopicOpinions(topic, parameterVo, pageNum, pageSize, parameterVo.getRepeatState());
        List<PoPublicOpinion> opinions = (List<PoPublicOpinion>) esSearchResult.get("opinions");
        total = (Long) esSearchResult.get("total");
        List<NameValue> languageTypeCounts = poPublicOpinionService.getOpinionByLanguageTypeCounts(opinions.stream().map(PoPublicOpinion::getId).collect(Collectors.toList()));
        TableDataInfo tableDataInfo = getDataTable(opinions);
        TableDataInfoCustom tableDataInfoCustom = new TableDataInfoCustom();
        tableDataInfoCustom.setCustom(languageTypeCounts);
        tableDataInfoCustom.setCode(tableDataInfo.getCode());
        tableDataInfoCustom.setMsg(tableDataInfo.getMsg());
        tableDataInfoCustom.setTotal(total);
        tableDataInfoCustom.setRows(tableDataInfo.getRows());
        return tableDataInfoCustom;
    }


    /**
     * 根据主题id获取舆情集合(分页)(语言)
     */
    @ApiOperation("根据主题id获取舆情集合(分页)")
    @PostMapping("/queryByTopicId/domestic/{topicId}")
    public TableDataInfoCustom queryBydomesticTopicId(@PathVariable("topicId") Long topicId, @RequestBody TopicParameterVo parameterVo) {
        Long total = 0L;
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        PublicOpinionTopic topic = publicOpinionTopicService.selectPublicOpinionTopicById(topicId);
        HashMap esSearchResult = esUtil.getTopicOpinions(topic, parameterVo, pageNum, pageSize, parameterVo.getRepeatState());
        List<PoPublicOpinion> opinions = (List<PoPublicOpinion>) esSearchResult.get("opinions");
        total = (Long) esSearchResult.get("total");
        TableDataInfo tableDataInfo = getDataTable(opinions);
        TableDataInfoCustom tableDataInfoCustom = new TableDataInfoCustom();
        tableDataInfoCustom.setCode(tableDataInfo.getCode());
        tableDataInfoCustom.setMsg(tableDataInfo.getMsg());
        tableDataInfoCustom.setTotal(total);
        tableDataInfoCustom.setRows(tableDataInfo.getRows());
        return tableDataInfoCustom;
    }


    /**
     * 根据事件id获取舆情媒体类型数量
     */
    @ApiOperation("根据事件id获取舆情媒体类型数量")
    @PreAuthorize("@ss.hasPermi('system:opinion:list')")
    @PostMapping("/queryMediaTypeByEventId/{eventId}")
    public AjaxResult queryMediaTypeByEventId(@PathVariable("eventId") Long eventId, @RequestBody EventParameterVo parameterVo) {
        List<CountResultVo> list = poPublicOpinionService.queryMediaTypeByEventId(eventId, parameterVo);
        return AjaxResult.success(list);
    }


    /**
     * 获取预警舆情媒体类型数量
     */
    @ApiOperation("获取预警舆情媒体类型数量")
    @PostMapping("/getForecastMediaType")
    public AjaxResult getForecastMediaType(@RequestBody OpinionParameterVo opinionParameterVo) {
        String tenantCode = SecurityUtils.getLoginUser().getUser().getTenantCode();
        opinionParameterVo.setTenantCode(tenantCode);
        List<NameValue> list = poPublicOpinionService.getForecastMediaType(opinionParameterVo);
        return AjaxResult.success(list);
    }




    /**
     * 舆情加入收藏
     */
    @ApiOperation("舆情加入收藏")
    @GetMapping("/addToFavorites/{ids}")
    public AjaxResult addToFavorites(@PathVariable String[] ids) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        String tenantCode = SecurityUtils.getLoginUser().getUser().getTenantCode();
        return toAjax(poPublicOpinionService.addToFavorites(ids, userId, deptId, tenantCode));
    }


    /**
     * 移除收藏
     */
    @ApiOperation("移除收藏")
    @GetMapping("/removeFavorites/{ids}")
    public AjaxResult removeFavorites(@PathVariable String[] ids) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        String tenantCode = SecurityUtils.getLoginUser().getUser().getTenantCode();
        return toAjax(poPublicOpinionService.removeFavorites(ids, userId, deptId, tenantCode));
    }


    /**
     * 收藏舆情列表
     */
    @ApiOperation("收藏舆情列表")
    @GetMapping("/getFavorites")
    public TableDataInfo getFavorites() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        String tenantCode = SecurityUtils.getLoginUser().getUser().getTenantCode();
        PageInfo<PoPublicOpinion> pageInfo = poPublicOpinionService.getFavorites(userId, deptId, tenantCode, pageNum, pageSize);
        TableDataInfo tableDataInfo = getDataTable(pageInfo.getList());
        tableDataInfo.setTotal(pageInfo.getTotal());
        return tableDataInfo;
    }






    /**
     * 预警总数
     */
    @ApiOperation("预警总数")
    @GetMapping("/getWarningCount")
    public AjaxResult getWarningCount(@RequestParam(value = "timeType", required = false) String timeType,
                                        @RequestParam(value = "beginPublishDay", required = false) String beginPublishDay,
                                        @RequestParam(value = "endPublishDay", required = false) String endPublishDay) {
        HashMap hashMap = DateUtils.getStartEndTime(timeType, beginPublishDay, endPublishDay);
        beginPublishDay = (String) hashMap.get("beginPublishDay");
        endPublishDay = (String) hashMap.get("endPublishDay");
        Long count = poPublicOpinionService.getWarningCount(beginPublishDay, endPublishDay);
        return AjaxResult.success(count);
    }


    /**
     * 负面信息热力图
     */
    @ApiOperation("负面信息热力图")
    @GetMapping("/negativeInformationHeatMap")
    public AjaxResult negativeInformationHeatMap(@RequestParam(value = "beginPublishDay", required = true) String beginPublishDay,
                                                 @RequestParam(value = "endPublishDay", required = true) String endPublishDay) {

        Map<String, Object> map = new HashMap();
        List<String> dateList = DateUtils.getDatesBetweenUsingJava7String(DateUtils.parseDate(beginPublishDay),DateUtils.parseDate(endPublishDay));
        EventParameterVo parameter = new EventParameterVo();
        if (StringUtils.isNotEmpty(beginPublishDay)) {
            parameter.setStartDateTime(DateUtils.parseDate(beginPublishDay));
        }
        if (StringUtils.isNotEmpty(endPublishDay)) {
            parameter.setEndDateTime(DateUtils.parseDate(endPublishDay));
        }
        List<NameListValue> list = poPublicOpinionService.groupSensitiveOpinionDayHour(SensitiveTypeEnum.Negative.name, parameter);
        map.put("dateList", dateList);
        map.put("heatData", list);
        return AjaxResult.success(map);
    }



    /**
     * 获取正负面信息趋势图
     */
    @ApiOperation(value = "获取正负面信息趋势图", notes = "获取正负面信息趋势图")
    @GetMapping("/getSentimentByPublishTime")
    public TableDataInfo getSentimentByPublishTime(@RequestParam(value = "beginPublishDay") String beginPublishDay,
                                                   @RequestParam(value = "endPublishDay") String endPublishDay) {
        List<NameListValue> list = poPublicOpinionService.getSentimentByPublishTime(beginPublishDay,endPublishDay);
        return getDataTable(list);
    }


    /**
     * 本年度分媒体综合统计
     */
    @ApiOperation("本年度分媒体综合统计")
    @GetMapping("/comprehensiveStatisticsThisYear")
    public AjaxResult comprehensiveStatisticsThisYear() {
        Map<String, Object> map = poPublicOpinionService.comprehensiveStatisticsThisYear();
        return AjaxResult.success(map);
    }


    /**
     * 本年度分媒体综合统计
     */
    @ApiOperation("获取所有媒体类型")
    @GetMapping("/getAllMediaType")
    public AjaxResult getAllMediaType() {
        List<String> allMediaTypes = poPublicOpinionService.getAllMediaType();
        return AjaxResult.success(allMediaTypes);
    }


}
